package com.budwk.app.sqfwV2.generator.binyangStreet.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskPdfDTO;
import com.budwk.app.sqfwV2.enums.BinYangFileServiceEnum;
import com.budwk.app.util.poiUtils.SitePictureOfficeUtils;
import com.deepoove.poi.XWPFTemplate;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 宾阳街道图片预览 生成pdf 2.0后操作
 */

@Slf4j
public class BingYangPictureGenerator extends BaseTemplatePdfGenerator {


    @Override
    public String getTemplateName() {
        return null;
    }

    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfGenerateParam) {
        return false;
    }

    @Override
    public ByteArrayOutputStream generatePDFAfter(XWPFTemplate xwpfTemplate, Sqfw_formSummary_new formSummary) {
        SafeCheckTaskPdfDTO pdfDTO = JSON.parseObject(formSummary.getParamData(), SafeCheckTaskPdfDTO.class);
        BinYangFileServiceEnum serviceEnum = BinYangFileServiceEnum.getByLabel(pdfDTO.getType(), formSummary.getDistrict(), formSummary.getCounty());
        if (serviceEnum == null) {
            return new ByteArrayOutputStream();
        }
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList = Lists.newArrayList();
        List<SafeCheckTaskParamDataDTO> hiddenDanger = pdfDTO.getHiddenDanger();
        if (hiddenDanger == null || hiddenDanger.size() == 0) {
            return new ByteArrayOutputStream();
        }
        String fillDateStr = DateUtil.format(formSummary.getFillDate(), DatePattern.CHINESE_DATE_PATTERN);
        List<String> yhList = new ArrayList<>();
        for (SafeCheckTaskParamDataDTO problem : hiddenDanger) {
            String riskImages = problem.getRiskImages();
            if (StringUtils.isEmpty(riskImages)) {
                continue;
            }
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(riskImages.split(",")));
            yhList.addAll(pictureList);

        }
        if (yhList.size() > 0) {
            String yhStr = String.format("%s%s%s%s%s", "附件：", pdfDTO.getPlaceName(), " ", fillDateStr, " 隐患图片");
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(yhStr, yhList));
        }
        // 走访图片
        String visitPicturesFileList = pdfDTO.getVisitPicturesFileList();
        if (StringUtils.isNotEmpty(visitPicturesFileList)) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(visitPicturesFileList.split(",")));
            String textStr = String.format("%s%s%s%s%s", "附件：", pdfDTO.getPlaceName(), " ", fillDateStr, " 走访图片");
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }

        ByteArrayOutputStream byteArrayOutputStream = SitePictureOfficeUtils.createSitePicture(sitePictureContentList);
        if (byteArrayOutputStream == null) return new ByteArrayOutputStream();
        return byteArrayOutputStream;

    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfParamBO) {

    }
}
